home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / math / fixfloa2 / c_demo.c < prev    next >
C/C++ Source or Header  |  1995-03-29  |  10KB  |  423 lines

  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. #include "fixfloat.h"
  5.  
  6. /*
  7.  
  8.   Examples of how to use fixpoint arithmetic functions
  9.   provided by fixfloat.asm.
  10.  
  11.   Compile under Watcom C/C++ 10.0, 32 bit Protected Mode
  12.  
  13.   Author :    Arne Steinarson, email arst@ludd.luth.se
  14.  
  15. */
  16.  
  17.  
  18.  
  19. // 
  20. // Small func to print a fixfloat with dec representation
  21. //
  22. void fix_outd(long ff){
  23.   char buf[20];
  24.  
  25.   fftoa(ff,buf);
  26.   puts(buf);
  27.   }
  28.  
  29. // 
  30. // Small func to print a fixfloat with hex representation
  31. //
  32. void fix_outh(long ff){
  33.   char buf[20];
  34.  
  35.   fftoah(ff,buf);
  36.   puts(buf);
  37.   }
  38.  
  39. // 
  40. // Small func to input a fixfloat with a lead text
  41. //
  42. long fix_in(char *lead){
  43.   float f;
  44.  
  45.   if(lead) puts(lead);
  46.   scanf("%f",&f);
  47.   return f*65536;
  48.   }
  49.  
  50.  
  51. void main(){
  52.  
  53.   long   a,b,c,d;
  54.  
  55.   printf("\nASSIGNMENT AND REPRESENTATION");
  56.   a = 3.1415 * 65536;
  57.   printf("\nassigning a = 3.1415 * 65536 \n");
  58.   printf("\n a in decimal form          %d",a);
  59.   printf("\n a in hexadecimal form      0x%X",a);
  60.   printf("\n a printed with fftoa conversion  "); fix_outd(a);
  61.   printf(" a printed with fftoah conversion 0x"); fix_outh(a);
  62.  
  63.  
  64.   getch();
  65.   printf("\nFIXFLOAT TO INTEGER");
  66.   printf("\nTaking integer part of a through a>>16 : %d\n", a>>16);
  67.  
  68.  
  69.   getch();
  70.  
  71.  
  72.   printf("\nADDITION");
  73.   a = 8.7 * 65536;
  74.   b = 2.2 * 65536;
  75.  
  76.   printf("\n a is now "); fix_outd(a);
  77.   printf(" b is now "); fix_outd(b);
  78.   printf(" Executing c = b + a :"); 
  79.   c = b + a;
  80.   printf("\n c is now "); fix_outd(c);
  81.  
  82.  
  83.   getch();
  84.  
  85.  
  86.   printf("\nINLINE ASSEMBLY SIGNED MULTIPLICATION");
  87.   a = 3.3 * 65536;
  88.   b = -3.0 * 65536;
  89.  
  90.   printf("\n a is now "); fix_outd(a);
  91.   printf(" b is now "); fix_outd(b);
  92.   printf(" Executing c = iffsmul(a, b) :"); 
  93.   c = iffsmul(a, b);
  94.   printf("\n c is now "); fix_outd(c);
  95.  
  96.  
  97.   getch();
  98.  
  99.  
  100.   printf("\nUNSIGNED MULTIPLICATION THROUGH FUNCTION CALL");
  101.   a = 3.3 * 65536;
  102.   b = 1000.0 * 65536;
  103.  
  104.   printf("\n a is now "); fix_outd(a);
  105.   printf(" b is now "); fix_outd(b);
  106.   printf(" Executing c = ffmul(a, b) :"); 
  107.   c = ffmul(a, b);
  108.   printf("\n c is now "); fix_outd(c);
  109.  
  110.  
  111.   getch();
  112.  
  113.  
  114.   printf("\nINLINE ASSEMBLY SIGNED DIVISION");
  115.   a = 15.5 * 65536;
  116.   b = -5.0 * 65536;
  117.  
  118.   printf("\n a is now "); fix_outd(a);
  119.   printf(" b is now "); fix_outd(b);
  120.   printf(" Executing c = iffsdiv(a, b) :"); 
  121.   c = iffsdiv(a, b);
  122.   printf("\n c is now "); fix_outd(c);
  123.  
  124.  
  125.   getch();
  126.  
  127.  
  128.   printf("\nUNSIGNED DIVISION THROUGH FUNCTION CALL");
  129.   a = 10.0 * 65536;
  130.   b = 4.0 * 65536;
  131.  
  132.   printf("\n a is now "); fix_outd(a);
  133.   printf(" b is now "); fix_outd(b);
  134.   printf(" Executing c = ffdiv(a, b) :"); 
  135.   c = ffdiv(a, b);
  136.   printf("\n c is now "); fix_outd(c);
  137.  
  138.  
  139.   getch();
  140.  
  141.  
  142.   printf("\nThe functions ffdiv & ffsdiv have protection ");
  143.   printf("\nagainst divide overflows, for example 7/0");
  144.   printf("\nThis simplifies programming sometimes but ");
  145.   printf("\ndivision takes longer time due to argument checking ");
  146.   printf("\n\nEXAMPLE : ");
  147.  
  148.   a = -7.0 * 65536;
  149.   b = 0.0 * 65536;
  150.  
  151.   printf("\n a is now "); fix_outd(a);
  152.   printf(" b is now "); fix_outd(b);
  153.   printf(" Executing c = ffsdiv(a, b) :"); 
  154.   c = ffsdiv(a, b);
  155.   printf("\n c is now "); fix_outd(c);
  156.  
  157.   getch();
  158.  
  159.  
  160.  
  161.  
  162.   printf("\nAs we move to transcendental functions");
  163.   printf("\nTables have to be initialized by gen_fixfloat_tables()");
  164.  
  165.   gen_fixfloat_tables();
  166.   getch();
  167.  
  168.  
  169.   printf("\n\n\nIn fixfloat universe there are 65536 degrees ");
  170.   printf("\non a circle ( integer perspective ) or 1 degree");
  171.   printf("\n( fixfloat perspective ). Here are some degrees ");
  172.   printf("\nin 360 degree scale, 1 degree scale and 65536");
  173.   printf("\ndegree scale\n");
  174.  
  175.   printf("\n360 CIRCLE   65536 CIRCLE    1.00 CIRCLE \n\n");
  176.  
  177.   for(a=0; a<=65536; a+=8192){  
  178.     printf(" %4d         %5d         ", a * 360 / 65536, a);
  179.     fix_outd(a);
  180.     }
  181.  
  182.   getch();
  183.  
  184.  
  185.   printf("\n\nTRIGONOMETRIC FUNCTIONS\n ");
  186.  
  187.   a = 45 * 65536 / 360;
  188.  
  189.   printf("\n a is now "); fix_outd(a);
  190.   printf("corresponing to %d degrees ", a*360/65536); 
  191.  
  192.   printf(" Executing c = ffsin(a) :"); 
  193.   c = ffsin(a);
  194.   printf("\n c is now "); fix_outd(c);
  195.  
  196.   getch();
  197.  
  198.   printf("\n Lets go the other way : ");
  199.   printf(" Executing a = ffasin(c) :"); 
  200.   a = ffasin(c);
  201.  
  202.   printf("\n a is now "); fix_outd(a);
  203.   printf("corresponing to %d degrees ", a*360/65536); 
  204.  
  205.   getch();
  206.  
  207.   printf("\n\nThe functions ffcos, ffacos, fftan, ffatan");
  208.   printf("\nworks in a similar way");
  209.  
  210.  
  211.   getch();
  212.  
  213.  
  214.   printf("\n\nThe function ff_vec_to_ang tells what direction");
  215.   printf("\na certain vector is pointing, the coordinate system");
  216.   printf("\nhave increasing x to the right and increasing y upward :");
  217.  
  218.   a = 4.0 *65536;
  219.   b = 2.0 * 65536;
  220.  
  221.   printf("\n a is now "); fix_outd(a);
  222.   printf(" b is now "); fix_outd(b);
  223.   printf(" Executing c = ff_vec_to_ang(a, b) :"); 
  224.   c = ff_vec_to_ang(a, b);
  225.   printf("\n c is now "); fix_outd(c);
  226.   printf("corresponing to %d degrees ", c*360/65536); 
  227.  
  228.  
  229.   getch();
  230.  
  231.  
  232.   printf("\n\nLOGARITHMIC & EXPONENTIAL FUNCTIONS\n ");
  233.     
  234.   a = 1024 *65536;
  235.  
  236.   printf("\n a is now "); fix_outd(a);
  237.   printf(" Executing c = fflog2(a) :"); 
  238.   c = fflog2(a);
  239.   printf("\n c is now "); fix_outd(c);
  240.  
  241.   getch();
  242.  
  243.   printf("\n Lets go the other way : ");
  244.   printf(" Executing a = ffexp2(c) :"); 
  245.   a = ffexp2(c);
  246.  
  247.   printf("\n a is now "); fix_outd(a);
  248.  
  249.   
  250.   getch();
  251.  
  252.  
  253.   printf("\nThere's also an exp function to raise ");
  254.   printf("\nan arbitrary number to another arbitrary");
  255.   printf("\nnumber, range is very limited with fixfloat though.");
  256.  
  257.   printf("\n\nEXPONENTIAL FUNTION");
  258.   a = 9.0 * 65536;
  259.   b = 2.5 * 65536;
  260.  
  261.   printf("\n a is now "); fix_outd(a);
  262.   printf(" b is now "); fix_outd(b);
  263.   printf(" Executing c = ffpow(a , b) :"); 
  264.   c = ffpow(a , b);
  265.   printf("\n c is now "); fix_outd(c);
  266.  
  267.   getch();
  268.  
  269.  
  270.   printf("\nSQUARE ROOT : ");
  271.   a = 625 * 65536;
  272.  
  273.   printf("\n a is now "); fix_outd(a);
  274.   printf(" Executing c = ffsqrt(a) :"); 
  275.   c = ffsqrt(a);
  276.   printf("\n c is now "); fix_outd(c);
  277.  
  278.  
  279.   getch();
  280.  
  281.  
  282.   printf("\n\nA sad fact in 16.16 fixfloat universe is that");
  283.   printf("\nwhen squaring a number >=256.0 32 bits will overflow.");
  284.   printf("\nSort of a cure is to use 64 bit intermediate ");
  285.   printf("\nquantities, because in the end one will want to");
  286.   printf("\ntake the root of it all or divide by a ");
  287.   printf("\nfairly large number. Asm language handles 64 bit"); 
  288.   printf("\nquantities well, not C however.");
  289.  
  290.   printf("\n\nFunctions ffhyp, fftrihyp, ffmmd, ffmuldiv");
  291.   printf("\nff_solve_2nd_poly will NEVER overflow because they");
  292.   printf("\nuse intermediate 64 bit quantities.");
  293.  
  294.   getch();
  295.  
  296.   printf("\n\n2 DIM DISTANCE FUNCTION ( c = sqrt( a*a + b*b )): ");
  297.   a = 3 * 65536;
  298.   b = 4 * 65536;
  299.  
  300.   printf("\n a is now "); fix_outd(a);
  301.   printf(" b is now "); fix_outd(b);
  302.   printf(" Executing c = ffhyp(a,b) :"); 
  303.   c = ffhyp(a,b);
  304.   printf("\n c is now "); fix_outd(c);
  305.  
  306.  
  307.   getch();
  308.  
  309.  
  310.   printf("\nMany times one needs just a very rough approximation");
  311.   printf("\nto the distance between 2 points, if an accuracy of");
  312.   printf("\n10 to 15 percent is enough, squaring & rooting is quite");
  313.   printf("\na waste of time. ffalmosthyp() might come in handy here");
  314.   printf("\nit executes in roughly 18 clocks and works just as");
  315.   printf("\nwell with integers input to it");
  316.  
  317.   getch();
  318.  
  319.   printf("\n\n2 DIM DISTANCE FUNCTION ( c = max(a, b) - min(a, b)/2): ");
  320.   a = 3 * 65536;
  321.   b = 4 * 65536;
  322.  
  323.   printf("\n a is now "); fix_outd(a);
  324.   printf(" b is now "); fix_outd(b);
  325.   printf(" Executing c = ffalmosthyp(a,b) :"); 
  326.   c = ffalmosthyp(a,b);
  327.   printf("\n c is now "); fix_outd(c);
  328.  
  329.  
  330.   getch();
  331.  
  332.  
  333.   printf("\n\n3 DIM DISTANCE FUNCTION ( c = sqrt( a*a + b*b + c*c)): ");
  334.   a = 3 * 65536;
  335.   b = 4 * 65536;
  336.   c = 4 * 65536;
  337.  
  338.   printf("\n a is now "); fix_outd(a);
  339.   printf(" b is now "); fix_outd(b);
  340.   printf(" c is now "); fix_outd(b);
  341.   printf(" Executing d = fftrihyp(a,b,c) :"); 
  342.   d = fftrihyp(a,b,c);
  343.   printf("\n d is now "); fix_outd(d);
  344.  
  345.  
  346.   getch();
  347.  
  348.  
  349.   printf("\n\n INTEGER SQUARE ROOT");
  350.  
  351.   a = 120000;
  352.  
  353.   printf("\n a is now %d",a );
  354.   printf(" Executing c = isqrt(a) :"); 
  355.   c = isqrt(a);
  356.   printf("\n c is now %d",c );
  357.  
  358.  
  359.   getch();
  360.  
  361.  
  362.   printf("\n\n RANDOM NUMBER FUNCTIONS ");
  363.  
  364.   printf("\nThere are three random number functions for");
  365.   printf("\ndifferent situations : ");
  366.  
  367.   printf("\n\nrand32       generates a full 32 bit random number");
  368.  
  369.   printf("\n\nrandinter    generates a random number in a");
  370.   printf("\n             speced interval");
  371.  
  372.   printf("\n\nrandpowint   does the same but gets some help by");
  373.   printf("\n             the user providing the 2 power value-1");
  374.   printf("\n             closest above the desired interval");
  375.   printf("\n             This makes it speedier.");
  376.  
  377.   getch();
  378.  
  379.   printf("\n\n Here comes a few 32 bit random numbers  ");
  380.   printf("\n generated by rand32() : ");
  381.  
  382.   for(a=0;a<10;a++)
  383.     printf("\n%10d",rand32());
  384.  
  385.   getch();
  386.  
  387.   printf("\n\n Here comes a few random numbers between 0 and 49 : ");
  388.   printf("\n generated by randinter(50) : ");
  389.  
  390.   for(a=0;a<10;a++)
  391.     printf("\n%3d",randinter(50));
  392.  
  393.   getch();
  394.  
  395.   printf("\n\n Here comes a few random numbers between 0 and 49 : ");
  396.   printf("\n generated by randpowint(63,50) : ");
  397.  
  398.   for(a=0;a<10;a++)
  399.     printf("\n%3d",randpowint(63,50));
  400.  
  401.  
  402.   getch();
  403.  
  404.  
  405.   printf("\n\nThere are more functions which are documented");
  406.   printf("\nin textfile fixfloat.doc.");
  407.   printf("\n\nIf you find bugs let me know so I can fix them. ");
  408.   printf("\nIf you write some nice fast asm functions ");
  409.   printf("\nworking with fixfloat numbers why not email ");
  410.   printf("\nCODE & DOC to me = arst@ludd.luth.se  ;-) .");
  411.  
  412.   printf("\n\n If your code dont execute ...");
  413.   printf("\n\n ... try compiling it !");
  414.   getch();
  415.   }
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.